<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:p="http://primefaces.org/ui"
      xmlns:f="http://java.sun.com/jsf/core">

    <ui:composition template="layout.xhtml">
        <ui:define name="body"> 
 
            <h:form id="reqForm" enctype="multipart/form-data">
               <br/>
                <h3 class="title">Cadastrando Requerimento</h3>
                <p:growl id="messages" showDetail="true" autoUpdate="true" /> 
                <div class="row">
                    <div class="twelve columns"> 
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Entrada</h4></legend>
                            <p:panel id="reqPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix both">

                                <p:outputLabel value="Processo:" styleClass="grid_3" for="processInputText"/>
                                <p:inputText id="processInputText" styleClass="grid_8" 
                                             value="#{managerCreateRequirement.requirement.process}" />

                                <p:outputLabel value="Tipo:" styleClass="grid_3" for="typeSelectOneMenu"/>
                                <div class="grid_8">
                                    <p:selectOneMenu id="typeSelectOneMenu" required="true" 
                                                     requiredMessage="O tipo de Requerimento é Obrigatório" 
                                                     value="#{managerCreateRequirement.requirement.type_requeriment}">
                                        <f:selectItem itemLabel="" itemValue=""/>
                                        <f:selectItems value="#{managerCreateRequirement.typeRequeriment}"/>  
                                        <p:ajax event="change"  update=":reqForm:docPanel "
                                                listener="#{managerCreateRequirement.ajaxTypeAnnex()}" />
                                    </p:selectOneMenu>
                                </div>
                                <div class="clear" /> 
                            </p:panel>

                        </fieldset>

                        <fieldset> 
                            <legend> <h4 style="margin:0 0 10px 0">Interessado</h4></legend>
                            <p:panel id="qualidadePanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix both "> 

                                <p:outputLabel value="Nome:" styleClass="grid_3" for="idINome"/>
                                <p:inputText id="idINome" styleClass="grid_20 ajustInput" required="true" requiredMessage="O Campo Nome Interessado é Obrigatório" 
                                             value="#{managerCreateRequirement.requirement.interested}" />
                                <p:outputLabel value="CPF:" styleClass="grid_3" for="cpfInputMask"/>
                                <p:inputMask id="cpfInputMask" mask="999.999.999-99" styleClass="grid_8" 
                                             value="#{managerCreateRequirement.requirement.cpfInterested}" />
                                
                                <p:outputLabel value="Qualidade:" styleClass="grid_3" for="typeQualitySelectOneMenu"/>
                                <div class="grid_8">
                                    <p:selectOneMenu id="typeQualitySelectOneMenu" required="true" 
                                                     requiredMessage="O tipo de Qualidade é Obrigatório" 
                                                     value="#{managerCreateRequirement.requirement.typeQuality}">
                                        <f:selectItem itemLabel="" itemValue=""/>
                                        <f:selectItems value="#{managerCreateRequirement.typeQuality}"/>  

                                    </p:selectOneMenu>
                                </div>
                                <div class="clear" /> 
                                <fieldset> 
                                    <legend> <h4 style="margin:0 0 10px 0">Endereço</h4></legend>
                                    <p:outputLabel value="Rua:" styleClass="grid_3" for="addressInterestedInputText"/>
                                    <p:inputText id="addressInterestedInputText" styleClass="grid_20 ajustInput" 
                                                 value="#{managerCreateRequirement.requirement.addressInterested}" />
                                    
                                    <p:outputLabel value="Bairro:" styleClass="grid_3" for="neighborhoodInterestedInputText" />
                                    <p:inputText id="neighborhoodInterestedInputText" styleClass="grid_8" 
                                                 value="#{managerCreateRequirement.requirement.neighborhoodInterested}" />
                                    <p:outputLabel value="CEP:" styleClass="grid_2" for="zipCodeNeighborhoodInterestedInputMask"/>
                                    <p:inputMask id="zipCodeNeighborhoodInterestedInputMask" mask="99.999-999" styleClass="grid_4" 
                                                 value="#{managerCreateRequirement.requirement.zipCodeNeighborhoodInterested}" />
                                    <p:outputLabel value="Nº:" styleClass="grid_2 alpha" for="numInterestedInputText"/>
                                    <p:inputText id="numInterestedInputText" styleClass="grid_3" 
                                                 value="#{managerCreateRequirement.requirement.numInterested}" />
                                </fieldset>
                            </p:panel>

                        </fieldset>

                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Proprietário</h4></legend>
                            <p:panel id="intPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix both ">

                                <p:outputLabel value="Pessoa:" styleClass="grid_3" for="typePerson"/>
                                <p:selectOneRadio id="typePerson" style="margin-top:-8px;" styleClass="grid_8"
                                                  value="#{managerCreateRequirement.proprietary.typeDocument}" >  
                                    <f:selectItems value="#{managerCreateRequirement.typeDocumentList()}" />
                                    <p:ajax event="change" update=":reqForm:intPanel" />
                                </p:selectOneRadio>

                                <div class="clear"/>

                                <p:outputLabel id="typeDocumentLabel" for="typeDocumentInput"
                                               value="#{managerCreateRequirement.proprietary.typeDocument}: " 
                                               styleClass="grid_3"/>

                                <p:inputMask id="typeDocumentInput"
                                             value="#{managerCreateRequirement.proprietary.typeDocument.type eq 'CPF' ? 
                                                      managerCreateRequirement.proprietary.cpf : managerCreateRequirement.proprietary.cnpj}"
                                             mask="#{managerCreateRequirement.proprietary.typeDocument.type eq 'CPF' ? 
                                                     '999.999.999-99':'99.999.999/9999-99'}" 
                                             styleClass="grid_8" > 
                                    <p:ajax event="blur" update=":reqForm:intPanel" 
                                            listener="#{managerCreateRequirement.existProprietary()}" />
                                </p:inputMask>
                                <div class="clear"/>
                                
                                <p:outputLabel value="Nome:" styleClass="grid_3" for="nomeInputText" 
                                               rendered="#{managerCreateRequirement.proprietary.typeDocument.type eq 'CPF'}"/>
                                <p:inputText id="nomeInputText" styleClass="grid_20 ajustInput"  value="#{managerCreateRequirement.proprietary.name}" 
                                             readonly="#{managerCreateRequirement.readOnlyInputs}" required="true" requiredMessage="O Campo Nome Proprietário é Obrigatório"
                                             rendered="#{managerCreateRequirement.proprietary.typeDocument.type eq 'CPF'}" />

                                <p:outputLabel value="Fantasia:" styleClass="grid_3"
                                               for="nameFantasyInputText" rendered="#{managerCreateRequirement.proprietary.typeDocument.type eq 'CNPJ'}" />
                                <p:inputText id="nameFantasyInputText" styleClass="grid_20 ajustInput"
                                             readonly="#{managerCreateRequirement.readOnlyInputs}"
                                             value="#{managerCreateRequirement.proprietary.name_fantasy}" 
                                             rendered="#{managerCreateRequirement.proprietary.typeDocument.type eq 'CNPJ'}" />
                            </p:panel>
                        </fieldset>
                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Imóvel</h4></legend>
                            <p:panel id="imoPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix ">

                                <p:outputLabel value="CEP:" styleClass="grid_3" for="cepInputMask"/>
                                <p:inputMask id="cepInputMask" mask="99.999-999"  styleClass="grid_4"
                                             value="#{managerCreateRequirement.address.cep}">
                                    <p:ajax event="blur" update=":reqForm:imoPanel" 
                                            listener="#{managerCreateRequirement.existAddress()}" />
                                </p:inputMask>
                                <div class="clear-both" />

                                <p:outputLabel value="Logradouro:" styleClass="grid_3" for="patioInputText" />
                                <p:inputText id="patioInputText" styleClass="grid_20 ajustInput" 
                                              value="#{managerCreateRequirement.address.patio}" 
                                              readonly="#{managerCreateRequirement.readOnlyInputsAddress}" />

                                <p:outputLabel value="Bairro:" styleClass="grid_3" for="districtInputText" />
                                <p:inputText id="districtInputText" styleClass="grid_20 ajustInput" 
                                              value="#{managerCreateRequirement.address.district}" 
                                              readonly="#{managerCreateRequirement.readOnlyInputsAddress}" />

                                <p:outputLabel value="Número:" styleClass="grid_3" for="numberImovelInputText"/>
                                <p:inputText id="numberImovelInputText" styleClass="grid_4" 
                                             value="#{managerCreateRequirement.requirement.numberImovel}" />

<!--                                <p:outputLabel value="Tipo:" styleClass="grid_3"/>
                                <p:inputText  styleClass="grid_4" value="#{managerCreateRequirement.address.typePatio}" 
                                              readonly="#{managerCreateRequirement.readOnlyInputsAddress}" />-->
                                
                                <p:outputLabel value="Fone:" styleClass="grid_3 phoneLabel" for="phoneReqInputMask"/>
                                <p:inputMask id="phoneReqInputMask" mask="(99) 9999-9999" styleClass="grid_5"
                                             value="#{managerCreateRequirement.requirement.foneImovel}" />
                                <div class="clear-both" />

                                <p:outputLabel value="Obs:" styleClass="grid_3" for="obsInputTextArea"/>
                                <p:inputTextarea id="obsInputTextArea" styleClass="grid_20 ajustInput" 
                                                  value="#{managerCreateRequirement.requirement.obs}" />

                            </p:panel>

                        </fieldset>

                        <fieldset>
                            <legend> <h4 style="margin:0 0 10px 0">Documentos</h4></legend>
                            <p:panel id="docPanel" style="background: none; border: none;" 
                                     styleClass="container_24 clearfix ">

                                <p:outputLabel value="Documento:" styleClass="grid_3" for="doc"/>
                                <div class="grid_19">
                                    <p:selectOneMenu id="doc" value="#{managerCreateRequirement.annex.type_annex}">
                                        <f:selectItem itemLabel="" itemValue=""/>
                                        <f:selectItems id="docItens" value="#{managerCreateRequirement.typeAnnex}"/>   
                                        <p:ajax event="change" process="docPanel" update="docPanel" 
                                                listener="#{managerCreateRequirement.ajaxTypeAnnex()}"/>
                                    </p:selectOneMenu>
                                </div>
                                <div class="clear"/>
                                <br/>
                                <p:fileUpload 
                                    mode="advanced"
                                    label="Anexar"
                                    fileUploadListener="#{managerCreateRequirement.handle}"
                                    multiple="true"
                                    auto="true" disabled="#{managerCreateRequirement.disableAnnexButton}"
                                    invalidFileMessage="Tipo de arquivo não suportado."
                                    invalidSizeMessage="Tamanho do arquivo não é suportado"
                                    uploadLabel="Enviar"
                                    cancelLabel="Cancelar"
                                    immediate="true"
                                    update="messages, tableAnnex, docItens"
                                    />
                                <div class="clear"/>
                                <br/>

                                <p:dataGrid id="tableAnnex" var="a" value="#{managerCreateRequirement.annexs}" 
                                            rowIndexVar="rowid"
                                            paginatorTemplate="{CurrentPageReport}  {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"  
                                            rows="6" paginator="true"  
                                            paginatorPosition="bottom"
                                            emptyMessage="Nenhum registro">  
                                 
                                    <p:panel header="#{a.type_annex.name}" style="text-align:center; width: 280px;">  

                                        <h:outputLink  
                                            value="#{a.type ne 'application/pdf' ? 'viewImage.xhtml':'show'}" title="#{a.description}"  target="_blank" >

                                            <div style="text-align: center; width: 20%;" class="grid_4">

                                                <p:graphicImage styleClass="image grid_3" rendered="#{a.type ne 'application/pdf'}"
                                                                style="margin-left:1%; width: 50px;"
                                                                url="show/?annex=#{a.id}" />

                                                <p:outputLabel  value="#{a.description.length() ge 10 ? a.description.substring(0, 10).concat('...') : 
                                                                         a.description.replace('.png','').replace(' ','_')}" 
                                                                style="margin-left:1%; width: 100px;" rendered="#{a.type ne 'application/pdf'}"/>

                                                <p:graphicImage styleClass="image grid_3" rendered="#{a.type eq 'application/pdf'}"
                                                                style="margin-left:1%; width: 100px;" 
                                                                url="assets/images/pdf.jpeg" />


                                                <p:outputLabel  value="#{a.description.length() ge 10 ? a.description.substring(0, 10).concat('...') : 
                                                                         a.description.replace('.pdf','').replace(' ','_')}" 
                                                                style="margin-left:1%; width: 100px;" rendered="#{a.type eq 'application/pdf'}"/>

                                            </div>
                                            <f:param name="annex" value="#{a.id}"/>
                                            <p:commandButton  style="width: 30px; height: 30px; float: right" 
                                                              title="remover" update="@form"
                                                              actionListener="#{managerCreateRequirement.removeAnnex(rowid,a)}"
                                                              icon="ui-icon-trash"/>
                                        </h:outputLink>
                                        <div class="clear"/>

                                    </p:panel>  

                                  
                                </p:dataGrid>   
                            </p:panel>

                        </fieldset>

                        <br/>
                        <div class="buttonAction">
                            <p:commandButton value="Salvar" icon="ui-icon-disk"
                                             update="@form"
                                             actionListener="#{managerCreateRequirement.saveRequeriment()}"
                                             styleClass="ui-priority-primary">
                                <!--<p:resetInput target=":reqForm:reqPanel"  />--> 
                            </p:commandButton>
                            <p:button href="indexrequirement.xhtml" value="Cancelar"  icon="ui-icon-trash" />
                        </div >
                    </div>
                </div>
            </h:form>
            <h:form id="dlgForm">
                <p:panel id="paneldlg">
                    <p:dialog id="iddlg" header="Impressão" widgetVar="dlg" modal="false"
                              showEffect="fade" hideEffect="fade" resizable="false" height="20">  
                        <h:outputText value="Deseja imprimir o Requerimento?" /> <br/> 
                        <f:facet name="footer">

                            <center>
                                <p:commandButton value="Sim" id="idb"
                                                 actionListener="#{managerCreateRequirement.showReport()}"
                                                 update="@form"
                                                 process=":dlgForm:panelDialog"                                                 ajax="false" 
                                                 icon="ui-icon-check"
                                                 onclick="this.form.target='_blank'; dlg.hide()"

                                                 />
                                <p:commandButton value="Não" process="@this" onclick="dlg.hide()" icon="ui-icon-close" />
                            </center>

                        </f:facet>
                    </p:dialog>   
                </p:panel>
            </h:form>
        </ui:define>
    </ui:composition>

</html>
